package edu.hcmus.sow.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.math.util.MathUtils;

public class StringUtil {

   public static String getPrefix(String str) {
      int index = StringUtils.lastIndexOf(str, '.');
      String temp = str.substring(0, index);

      return temp;
   }

   public static String getSuffix(String str) {
      int index = StringUtils.lastIndexOf(str, '.');
      String temp = str.substring(index);

      return temp;
   }

   public static String bytesToString(long bytes) {
      if (bytes <= 1024)
         return "1.0KB";
      String[] _levels = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
      int index = (int) Math.floor(Math.log(bytes) / Math.log(1024));
      return MathUtils.round((bytes / Math.pow(1024, index)), 2) + _levels[index];
   }

   public static Integer convertTimeSpentToInt(String timeSpent) {
      String[] arr = timeSpent.split(":");
      return (Integer.parseInt(arr[0]) * 60 + Integer.parseInt(arr[1]));
   }

   public static String convertTimeSpentToString(int total) {
      String timeSpent = "";
      int hours = 0;
      while (total >= 60) {
         hours++;
         total -= 60;
      }
      if (total < 10) {
         timeSpent += hours + ":0" + total;
      } else {
         timeSpent += hours + ":" + total;
      }
      if (hours < 10)
         timeSpent = "0" + timeSpent;
      return timeSpent;
   }

   public static String convertCalendarToDate(Calendar cal) {
      String str = "";
      if (cal != null) {
         Date date = cal.getTime();
         SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
         str = formatter.format(date);
      }
      return str;
   }

   public static Calendar convertStringToCalendar(String str_date) {
      Calendar cal = Calendar.getInstance();
      try {
         SimpleDateFormat formater = new SimpleDateFormat("MM/dd/yyyy");
         Date date;
         date = (Date) formater.parse(str_date);
         cal.setTime(date);
      } catch (Exception e) {
         return null;
      }
      return cal;
   }

   public static String subStringToDisplay(String content, int length) {
      String temp = "";
      if (content != null) {
         if (content.length() > length) {
            temp = content.substring(0, length);
            temp += "   ...";
         } else
            temp = content;
      }
      return temp;
   }

   public static String removeHTML(String htmlString) {
      // Remove HTML tag from java String
      String noHTMLString = htmlString.replaceAll("\\<.*?\\>", "");

      // Remove Carriage return from java String
      noHTMLString = noHTMLString.replaceAll("\r", "<br/>");

      // Remove New line from java string and replace html break
      noHTMLString = noHTMLString.replaceAll("\n", " ");
      noHTMLString = noHTMLString.replaceAll("\'", "&#39;");
      noHTMLString = noHTMLString.replaceAll("\"", "&quot;");
      return noHTMLString;
   }
}
